using System;
using System.Collections;
using System.Collections.Generic;
using System.Html;
using System.Runtime.CompilerServices;
using Awaiter;
using Nsb.Classes;
using jQueryApi;

namespace YesPlayLove.Clusters
{
    [IgnoreNamespace]
    public class EditListHtml : EditHtml
    {

        public EditListHtml(Await awp, jQueryObject edEl) : base(awp, edEl)
        {
        }

        public static void Aw(Await awp, jQueryObject edEl)
        {
            new EditListHtml(awp, edEl);
        }

        protected override void Cont(jQueryEvent e)
        {
            new Await().AddDx(delegate { EdEl.Closest(".ItemJar").Children(".SimpleAddLineBtn").First().Click(); }).Commit();
        }

        protected override void Next(jQueryEvent e)
        {
            CancelEvent(e);
            jQueryObject all = EdEl.Closest(".OuterJar").Find("h2,li");
            int i = all.Index(EdEl[0]);
            jQueryObject next = all.Slice(i + 1).First();

            EdEl.Blur();
            new Await().AddDx(
                delegate
                {
                    if (i >= 0 && i < (all.Length - 1))
                        next.Click();
                    else
                        all.First().Click();
                }
                ).Commit();
        }

        protected override void Prior(jQueryEvent e)
        {
            CancelEvent(e);
            jQueryObject all = EdEl.Closest(".OuterJar").Find("h2,li");
            int i = all.Index(EdEl[0]);
            jQueryObject next = all.Slice(i - 1).First();

            EdEl.Blur();
            new Await().AddDx(
                delegate
                {
                    if (i > 0)
                        next.Click();
                    else
                        all.Last().Click();
                }
                ).Commit();
        }

        public static Dictionary ScoopData(jQueryObject elToScoop)
        {
            Dictionary data = new Dictionary();

            jQuery.Select(".SectionJar", elToScoop).Each(
                delegate(int i, Element ee)
                {
                    Dictionary sect = new Dictionary();
                    data["Section" + i] = sect;
                    sect["type"] = "ol";
                    sect["title"] = jQuery.This.Find("h2").Plugin<Jewl>().GetFValueString();
                    List<string> li = new List<string>();
                    sect["li"] = li;
                    jQuery.This.Find("li").Each(
                        delegate
                        {
                            li.Add(jQuery.This.Plugin<Jewl>().GetFValueString());
                            return true;
                        }
                        );
                    return true;
                }
                )
                ;
            return data;
        }

        public static void ShowHtmlListAw(Await awp, Dictionary features, jQueryObject appendTo)
        {
            new Await()
                .AddAw(FileBlobBase.GetBlobAw, features)
                .AddDx(
                    delegate(Await aw)
                    {
                        appendTo.AddClass("Jar OuterJar");
                        Dictionary<string, object> data = (Dictionary) aw[PkgBase.ResultRx];

                        foreach (KeyValuePair<string, object> p in data)
                        {
                            Dictionary<string, string> sect = (Dictionary) p.Value;

                            jQueryObject sj = jQuery.FromHtml("<div class='Jar SectionJar' style='position:relative'/>").AppendTo(appendTo);

                            jQuery.FromHtml("<h2/>").Html(sect["title"]).AppendTo(sj);

                            jQueryObject lj = jQuery.FromHtml("<div class='Jar ListJar' style='position:relative'/>").AppendTo(sj);

                            jQueryObject ls = jQuery.FromHtml("<" + Script.Value(sect["type"], "ul") + "/>")
                                .AppendTo(lj);

                            foreach (string s in (List<string>) (object) sect["li"])
                            {
                                jQueryObject ij = jQuery.FromHtml("<div class='Jar ItemJar' style='position:relative'/>").AppendTo(ls);

                                jQuery.FromHtml("<li/>")
                                    .AppendTo(ij)
                                    .Html(s)
                                    ;
                            }
                        }
                    }
                )
                .Commit(awp);
        }
    }
}